package common

import (
	"api-gateway-core/mapping"
	"dubbo.apache.org/dubbo-go/v3/config"
	"dubbo.apache.org/dubbo-go/v3/config/generic"
	"github.com/dubbogo/triple/pkg/common/constant"
)

type Configuration struct {
	HostName           string
	Port               string
	AppName            string
	RegistryConfigMap  map[string]*config.RegistryConfig
	ReferenceConfigMap map[string]*config.ReferenceConfig
	HttpStatements     map[string]*mapping.HttpStatement
	MapperRegistry     ReferenceSolution
}

func (this *Configuration) GetStatement(key string) *mapping.HttpStatement {
	return this.HttpStatements[key]
}
func (this *Configuration) AddStatement(statement *mapping.HttpStatement) {
	this.HttpStatements[statement.URI+"-"+string(statement.HttpCommandType)] = statement
}

func (this *Configuration) GetReference(statement *mapping.HttpStatement) (*generic.GenericService, error) {
	return this.MapperRegistry.GetReference(statement)
}

func (this *Configuration) AddReference(httpStatement *mapping.HttpStatement) error {
	return this.MapperRegistry.AddGenericReference(httpStatement)
}
func NewConfiguration(hostName, port string) *Configuration {
	return &Configuration{HostName: hostName, Port: port}
}

func (this *Configuration) RegistryConfiguration(address, applicationName, interfaceName, version string) {
	registryConfig := config.NewRegistryConfigBuilder().
		SetProtocol(constant.TRIPLE).
		SetTimeout("3000").
		SetAddress(address).Build()
	referenceConfig := config.NewReferenceConfigBuilder().
		SetGeneric(true).
		SetVersion(version).
		SetSerialization("hessian2").
		SetInterface(interfaceName).
		SetProtocol(constant.TRIPLE).Build()
	this.AppName = applicationName
	this.RegistryConfigMap = map[string]*config.RegistryConfig{applicationName: registryConfig}
	this.ReferenceConfigMap = map[string]*config.ReferenceConfig{interfaceName: referenceConfig}
	this.HttpStatements = make(map[string]*mapping.HttpStatement)
	//this.RegistryConfigMap[applicationName] = registryConfig
	//this.ReferenceConfigMap[interfaceName] = referenceConfig
}
